import {useRef} from 'react'
import * as THREE from 'three'
import Zjl from '../ping.module.css'
let App=({intersectsRef,canvasRef,lastTouchY})=>{
    let isDragging = useRef(false);
        //鼠标按下时
    let handleMouseDownJin=()=>{
        if(intersectsRef.current){
            isDragging.current=true
        }
    }
    
    let handleMouseMoveJin = (event) => {
        const touch = event.touches[0];
        let jiz=touch.clientY-lastTouchY.current
        lastTouchY.current=touch.clientY
        if (isDragging.current && intersectsRef.current) {
            // jiz 
            const positionAttribute = intersectsRef.current;
            positionAttribute.rotation.y+= jiz*0.02
                // 遍历所有顶点
            // for (let i = 0; i < positionAttribute.count; i++) {
            //         // 获取当前顶点坐标
            //     const vertex = new THREE.Vector3();
            //     vertex.fromBufferAttribute(positionAttribute, i);
        
            //         // 如果是上面的顶点（y > 0），增加其 y 坐标
            //         if (vertex.z > 0) {
            //             vertex.z += scaleFactorZ;  // 增长的量
            //         } 
            //         if (vertex.x > 0) {
            //             vertex.x += 0;  // 增长的量
            //         } 
            //         // 更新顶点坐标
            //     positionAttribute.setXYZ(i, vertex.x, vertex.y, vertex.z);
            // }
        
                // 需要通知几何体更新
            // positionAttribute.needsUpdate = true;
        }
    }
    
        //鼠标抬起时
    let handleMouseUpJin=()=>{
        isDragging.current =false
        canvasRef.current.children[0].removeEventListener('touchstart', handleMouseDownJin);
        canvasRef.current.children[0].removeEventListener('touchmove', handleMouseMoveJin);
        canvasRef.current.children[0].removeEventListener('touchend', handleMouseUpJin);
    }
    let long=()=>{
        canvasRef.current.children[0].addEventListener('touchstart', handleMouseDownJin);
        canvasRef.current.children[0].addEventListener('touchmove', handleMouseMoveJin);
        canvasRef.current.children[0].addEventListener('touchend', handleMouseUpJin);
    }
    return (
        <>
            <h4 onClick={long}  className={`${Zjl.h4}`}>旋转</h4>
        </>
    )
}
export default App